/**
 * @file drv_motor.h
 * @brief 电机封装驱动
 * @author wangh (wanghuan3037@fiberhome.com)
 * @version 1.0
 * @date 2020-08-10
 * @copyright Copyright (c) 2020  烽火通信
 */
#ifndef __DRV_MOTOR_H
#define __DRV_MOTOR_H 
#include "project_config.h"
#if BOARD_MOTOR_EN
#include <stdint.h>
#include <stdbool.h>
#include "bsp_gpio.h"
#include "FreeRTOS.h"
#include "task.h"
#include "semphr.h"
#include "timers.h"
#include "bsp_button.h"

/**
 * @brief 电机动作
 */
typedef enum {
	M_STOP = 0,
	M_FORWARD = 1,
	M_BACK = 2,
	M_RESET = 3,
	
}MOTOR_ACTION;

typedef enum {
	M_START_STOP = 0,	/* 未启动或启动完成 */
	M_START_READY = 1,	/* 需要启动 */
	M_START_RUN = 2,	/* 启动中（即处于启动延迟） */
	
}MOTOR_START_STATE;

/**
 * @brief 位置信息
 */
typedef enum {
	M_UNKNOWN = 0,
	M_UP = 1,
	M_DOWN = 2,
	M_CENTER = 3,
	
}MOTOR_LOCATION;

/**
 * @brief 电机信息结构体
 */
typedef struct MOTORS_CONTROL
{
	uint8_t index;
	
	Gpio_t Motor_ph1;
	PinNames Motor_ph1_pin;
	Gpio_t Motor_ph2;
	PinNames Motor_ph2_pin;
	Gpio_t Motor_set;
	PinNames Motor_set_pin;			/* 拨码引脚 */
	Gpio_t M_sensor_int;
	PinNames M_sensor_int_pin;		/* 传感引脚 */
	
    MOTOR_ACTION move_state;		/* 移动动作 */
	bool move_reset;				/* 复位状态（true:复位） */
	MOTOR_START_STATE move_start;	/* 启动状态 */
	
	MOTOR_LOCATION location_value;	/* 当前位置 */
	char location_key[5];			/* 当前位置flash存储键值 */

	uint8_t group_value;			/* 电机组号 */
	char group_key[5];				/* 电机组号flash存储键值 */
	
	SemaphoreHandle_t state_lock;		/* 电机状态访问锁 */
	TimerHandle_t mtr_autoStop_timer;	/* 电机动作超时计时器，用于自动关断 */
	TimerHandle_t mtr_start_timer;		/* 电机动作启动计时器，用于启动阶段计时 */
	
	struct MOTORS_CONTROL *prev, *next;
}MOTORS_CONTROL_S;


/**
 * @brief 电机驱动结构体
 */
typedef struct sMotorDriver
{
	#if USE_BSP_BUTTON
	void ( *Init )( MOTORS_CONTROL_S *, button_handler_t );
	#else
    void ( *Init )( MOTORS_CONTROL_S *, GpioIrqHandler * );
	#endif
	
	void ( *create_move_task) (void);
	
	void ( *move )(MOTORS_CONTROL_S *, MOTOR_ACTION);
	
	void ( *sensor_handle) (MOTORS_CONTROL_S *);

}tMotorDriver;

/**
 * @brief 驱动初始化获取驱动实例
 * @return tMotorDriver* 返回驱动实例指针
 */
tMotorDriver* MotorDriverInit( MOTORS_CONTROL_S *mtr, uint8_t mtr_num );

#endif	/* BOARD_MOTOR_EN */ 

#endif	 // __DRV_MOTOR_H 

/*********** (C) COPYRIGHT 2020 FiberHome *****END OF FILE****/
